package com.winai.launcher.override.dialog_global;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ks.projectbasictools.base.SimpleView;
import com.winai.launcher.R;
import com.winai.launcher.utils.BaseUtils;

/**
 * 作者：康少
 * 时间：2020/5/15 0015
 * 说明：自定义弹窗
 */
public class GlobalDialog extends Dialog {
    private Context mContext;

    /*控件对象*/
    private TextView dialog_title;
    private TextView tv_content;
    private TextView tv_confirm;
    private TextView tv_cancel;
    private View view_line2;

    private GlobalDialog(Context mContext) {
        super(mContext);
        this.mContext = mContext;
        //去除标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        SimpleView dialogView = new SimpleView(mContext, R.layout.dialog_tip);
        //将自定义布局设置进去
        setContentView(dialogView);
        findId(dialogView);
        initDefault();
    }

    private void findId(View view) {
        dialog_title = view.findViewById(R.id.dialog_title);
        tv_content = view.findViewById(R.id.tv_content);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        view_line2 = view.findViewById(R.id.view_line2);//纵线
    }

    private void initDefault() {
        setCancelAble(true);
        setOnCancelClickListener(null);
        setOnConfirmClickListener(null);
    }

    public void setTitle(String title) {
        dialog_title.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);
        dialog_title.setText(title);
    }

    public void setContent(String content) {
        tv_content.setText(content);
    }

    public void setConfirmText(String confirmText) {
        tv_confirm.setVisibility(TextUtils.isEmpty(confirmText) ? View.GONE : View.VISIBLE);
        tv_confirm.setText(confirmText);
        view_line2.setVisibility(TextUtils.isEmpty(tv_cancel.getText().toString())
                || TextUtils.isEmpty(confirmText) ? View.GONE : View.VISIBLE);
    }

    public void setCancelText(String cancelText) {
        tv_cancel.setText(cancelText);
        tv_cancel.setVisibility(TextUtils.isEmpty(cancelText) ? View.GONE : View.VISIBLE);
        view_line2.setVisibility(TextUtils.isEmpty(tv_confirm.getText().toString())
                || TextUtils.isEmpty(cancelText) ? View.GONE : View.VISIBLE);
    }

    public void setConfirmGradient(boolean confirmGradient) {
        if (confirmGradient)
            BaseUtils.setTextGradient(tv_confirm);
    }

    public void setCancelGradient(boolean cancelGradient) {
        if (cancelGradient)
            BaseUtils.setTextGradient(tv_cancel);
    }

    /**
     * 设置点击外围是否关闭弹窗
     *
     * @param cancelAble
     */
    public void setCancelAble(boolean cancelAble) {
        setCancelable(cancelAble);
    }

    public void setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowing()) dismiss();
                if (onConfirmClickListener != null)
                    onConfirmClickListener.onClick(GlobalDialog.this, v);
            }
        });
    }

    public void setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowing()) dismiss();
                if (onCancelClickListener != null)
                    onCancelClickListener.onClick(GlobalDialog.this, v);
            }
        });
    }

    public static class Builder {
        private Context mContext;

        private GlobalDialog mGlobalDialog;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder setTitle(String title) {
            create().setTitle(title);
            return this;
        }

        public Builder setContent(String content) {
            create().setContent(content);
            return this;
        }

        public Builder setConfirmText(String confirmText) {
            create().setConfirmText(confirmText);
            return this;
        }

        public Builder setCancelText(String cancelText) {
            create().setCancelText(cancelText);
            return this;
        }

        public Builder setConfirmGradient(boolean confirmGradient) {
            create().setConfirmGradient(confirmGradient);
            return this;
        }

        public Builder setCancelGradient(boolean cancelGradient) {
            create().setCancelGradient(cancelGradient);
            return this;
        }

        public Builder setCancelAble(boolean cancelAble) {
            create().setCancelAble(cancelAble);
            return this;
        }

        public Builder setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
            create().setOnConfirmClickListener(onConfirmClickListener);
            return this;
        }

        public Builder setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
            create().setOnCancelClickListener(onCancelClickListener);
            return this;
        }

        public GlobalDialog create() {
            if (mGlobalDialog == null) {
                mGlobalDialog = new GlobalDialog(mContext);
            }
            return mGlobalDialog;
        }

        public GlobalDialog show() {
            create();
            addParams();
            return mGlobalDialog;
        }

        private void addParams() {
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = mGlobalDialog.getWindow();
            if (window != null) {
                lp.copyFrom(window.getAttributes());
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
                if (mContext instanceof Activity) {
                    if (!((Activity) mContext).isFinishing()) {
                        mGlobalDialog.show();
                    }
                }
                window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                window.setAttributes(lp);
            }
        }
    }

    public interface OnConfirmClickListener {
        void onClick(GlobalDialog mGlobalDialog, View v);
    }

    public interface OnCancelClickListener {
        void onClick(GlobalDialog mGlobalDialog, View v);
    }
}
